package com.ljx.qqpro.controller;

import java.util.Arrays;
import java.util.Map;

import com.ljx.qqpro.common.utils.UserHolder;
import com.ljx.qqpro.vo.user.LogUserVo;
import com.ljx.qqpro.vo.user.RegUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ljx.qqpro.entity.UserEntity;
import com.ljx.qqpro.service.UserService;
import com.ljx.qqpro.common.utils.PageUtil;
import com.ljx.qqpro.common.utils.Result;



/**
 * 用户表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-10-18 17:28:13
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtil page = userService.queryPage(params);

        return Result.success(page, "page");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{username}")
    public Result<UserEntity> info(@PathVariable("username") String username){
		UserEntity user = userService.getUserInfo(username);

        return Result.success(user, "user");
    }

    /**
     * 获取用户自己的信息
     * @return
     */
    @GetMapping("/info")
    public Result<UserEntity> getUserInfo(){

        return Result.success(UserHolder.getUser(), "user");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody UserEntity user){
		userService.save(user);

        return Result.success(null, "");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody UserEntity user){
		userService.updateById(user);

        return Result.success(null, "");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "");
    }

    @PostMapping("/login")
    public Result<String> login(@RequestBody LogUserVo logUserVo){
        String token = userService.login(logUserVo);

        return Result.success(token, "登陆成功");
    }


    @PostMapping("/register")
    public Result<Object> register(@RequestBody RegUserVo regUserVo){
        userService.register(regUserVo);

        return Result.success(null, "注册成功");
    }
}
